package com.example.clock;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MySQLite extends SQLiteOpenHelper {

    public String updateSql;

    public MySQLite(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MySQLite(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    public void setUpdateSql(String updateSql) {
        this.updateSql = updateSql;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 2020 ; i < 2045 ; i++){
            String sql = "create table  if not exists`"+"B"+i+"` (\n" +
                    "\t`"+Constant.tabId+"` int ,\n" +
                    "\t`"+Constant.tabYear+"` int ,\n" +
                    "\t`"+Constant.tabMonth+"` int ,\n" +
                    "\t`"+Constant.tabDay+"` int ,\n" +
                    "\t`"+Constant.tabDepartment+"` String ,\n" +
                    "\t`"+Constant.tabWorkTime+"` int \n" +
                    "); ";
            db.execSQL(sql);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(updateSql);
    }
}
